#include <iostream>  
#include <windows.h>  
#include <thread>  
#include <fstream>  
#include <sstream>  
#include <string> 
#include <algorithm> 
#include <map>  

// 读取ini文件
class Config {  
public:  
    void load(const std::string& filename) {  
        std::ifstream file(filename);  
        if (!file) {  
            std::cerr << "Could not open the file: " << filename << std::endl;  
            return;  
        }  

        std::string line, current_section;  

        while (std::getline(file, line)) {  
            // 删除空格  
            line.erase(remove_if(line.begin(), line.end(), isspace), line.end());  

            // 跳过注释行  
            if (line.empty() || line[0] == ';') {  
                continue;  
            }  

            // 处理节  
            if (line[0] == '[' && line.back() == ']') {  
                current_section = line.substr(1, line.size() - 2);  
            }  
            // 处理键值对  
            else {  
                auto delimiter_pos = line.find('=');  
                if (delimiter_pos != std::string::npos) {  
                    std::string key = line.substr(0, delimiter_pos);  
                    std::string value = line.substr(delimiter_pos + 1);  
                    config_data[current_section][key] = value;  
                }  
            }  
        }  
    }  

    std::string get(const std::string& section, const std::string& key, const std::string& default_value = "") {  
        if (config_data.find(section) != config_data.end() && config_data[section].find(key) != config_data[section].end()) {  
            return config_data[section][key];  
        }  
        return default_value;  
    }  

private:  
    std::map<std::string, std::map<std::string, std::string>> config_data;  
};

// 检测键盘并作出反应
class Valorant{

private: 
    // 背闪
    int dodge_flashes_movement_length = 0;  // 背闪长度
    int dodge_flashes_movement_time   = 0;  // 背闪时间

    // 躲避
    int stupid_movement_maxinum_x     = 0;  // 最大x距离
    int stupid_movement_maxinum_y     = 0;  // 最大y距离
    int stupid_movement_time          = 0;  // 运动时间

public: 
    Valorant(const std::string& filename){
        Config config;  
        config.load(filename); // 加载配置文件  

        // 读取配置文件
        dodge_flashes_movement_length = std::stoi(config.get("dodge_flashes", "movement_length", "0")); 
        dodge_flashes_movement_time   = std::stoi(config.get("dodge_flashes", "movement_time", "0")); 

        stupid_movement_maxinum_x     = std::stoi(config.get("stupid", "movement_maxinum_x", "0"));  // 最大x距离
        stupid_movement_maxinum_y     = std::stoi(config.get("stupid", "movement_maxinum_y", "0"));  // 最大y距离
        stupid_movement_time          = std::stoi(config.get("stupid", "movement_time", "0"));  // 运动时间

    }

    void move_mouse(int dx, int dy, int duration) {  
        int intervals = duration * 1; // 持续时间转换为10ms为单位  

        // 如果时间过短，避免除以零  
        if (intervals == 0) {  
            return;  
        }  

        // 使用浮点数来计算增量  
        double step_x = static_cast<double>(dx) / intervals;  
        double step_y = static_cast<double>(dy) / intervals;  

        // std::cout << "----- " << step_x << " " << step_x << std::endl;

        for (int i = 0; i < intervals; ++i) {  
            POINT p;  
            GetCursorPos(&p); // 获取当前鼠标位置  
            SetCursorPos(static_cast<int>(p.x + step_x), static_cast<int>(p.y + step_y)); // 移动鼠标  
            std::this_thread::sleep_for(std::chrono::milliseconds(1)); // 暂停10毫秒  
        }  
    }  

    // 背闪
    void dodge_flashes() {  
        // 移动鼠标到右侧200像素  
        move_mouse(dodge_flashes_movement_length, 0, dodge_flashes_movement_time); // 0.1秒 = 100毫秒  
        
        // 返回原位置  
        move_mouse(-1 * dodge_flashes_movement_length, 0, dodge_flashes_movement_time);  
    }  

    // 躲避
    void stupid(){
        int x = std::rand() % (stupid_movement_maxinum_x * 2) - 999;
        int y = std::rand() % (stupid_movement_maxinum_y * 2) - 999;
        // std::cout << x << " " << y << std::endl;
        move_mouse(x, y, stupid_movement_time);  
    }

    void Run(){
        while (true) {  
            if (GetAsyncKeyState(VK_RETURN)) { // 检测到“Enter”键  
                dodge_flashes();  
            }  
            if (GetAsyncKeyState(VK_OEM_5)) { // 检测到“\”键  
                stupid(); 
            }  
            // if (GetAsyncKeyState(VK_OEM_5)) { // 检测到“Esc”键  
            //     break; // 停止程序  
            // }  
            std::this_thread::sleep_for(std::chrono::milliseconds(10)); // 主循环延迟  
        }  
    }

};

int main() {  
    std::cout << "Press 'Enter' to move the mouse. Press 'Esc' to stop." << std::endl;  
    std::srand(static_cast<unsigned int>(std::time(0))); // 设置随机种子，以当前时间为基础

    Valorant valorant = Valorant("Config.ini");
    valorant.Run();

    return 0;  
}